package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.ProfessorDAO;
import entity.Professor;

@WebServlet(name = "ProfessorController", urlPatterns ={ "/professorInsert",
		"/professorUpdate", "/professorDelete", "/professorList" })
public class ProfessorController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ProfessorController() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String action = request.getParameter("action");
		List<Professor> professores = null;
		
		switch (action) {
		
		case "confirmDelete":
			String par = request.getParameter("id");
			request.setAttribute("id", par);
			request.getRequestDispatcher(
					"/view/private/professor/confirmDelete.jsp").forward(request,
					response);
			break;

		case "list":	
			professores = ProfessorDAO.getAll();
			request.setAttribute("lista", professores);
			
			Integer totalLogados = (Integer) getServletContext().getAttribute(
					"qde_Users");
			request.setAttribute("totalLogados", totalLogados);
			
			request.getRequestDispatcher("/view/private/professor/lista.jsp")
				.forward(request, response);
			break;
		
		case "altera":
			
			professores = ProfessorDAO.findById(new Integer(request.getParameter("id")));
			request.setAttribute("lista", professores);
			request.getRequestDispatcher("/view/private/professor/altera.jsp").forward(request, response);
			
			break;
			
		case "delete":
			
		if (ProfessorDAO.delete(new Integer(request.getParameter("id")))){
			response.sendRedirect("" + request.getContextPath()
					+ "/professorList?action=list");
				} else {
					response.sendRedirect("" + request.getContextPath()
					+ "/professorList?action=list");
				}
		break;
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		Professor professor = new Professor();

		switch (action) {
			
			case "insert":

			professor.setNome(request.getParameter("nome"));
			professor.setCpf(request.getParameter("cpf"));
			professor.setEndereco(request.getParameter("endereco"));
			professor.setEmail(request.getParameter("email"));
			professor.setTelefone(request.getParameter("telefone"));

			if (ProfessorDAO.insert(professor)) {
				response.sendRedirect("" + request.getContextPath()
						+ "/professorList?action=list");
			} else {
				response.sendRedirect("" + request.getContextPath()
						+ "/msg_erro.jsp");
			}

			break;
			
			case "update":
				professor.setId(new Integer(request.getParameter("id")));
				professor.setCpf(request.getParameter("cpf"));
				professor.setNome(request.getParameter("nome"));
				professor.setEndereco(request.getParameter("endereco"));
				professor.setEmail(request.getParameter("email"));
				professor.setTelefone(request.getParameter("telefone"));
				
				if(ProfessorDAO.update(professor)){
					//request.setAttribute("msg", "Professor Alterado com Sucesso!");
				response.sendRedirect("" + request.getContextPath()
						+ "/professorList?action=list");
				}else{
					response.sendRedirect("" + request.getContextPath()
							+ "/professorList?action=list");
				}

				break;
		}
	}
}
